<template>
  <div class="page-container">
    <!-- 顶部返回按钮 -->
    <div class="header">
      <div class="back-btn" @click="goBack">
        <Left />
      </div>
      <h1 class="title">注册</h1>
      <div class="placeholder"></div>
    </div>

    <!-- 注册表单 -->
    <div class="register-form">
      <div class="form-header">
        <h2>创建账户</h2>
        <p>填写以下信息完成注册</p>
      </div>

      <div class="form-content">
        <div class="form-item">
          <label class="form-label">手机号</label>
          <div class="phone-input">
            <nut-input 
              v-model="formData.phone" 
              placeholder="请输入手机号"
              type="tel"
              class="form-input"
            />
            <nut-button 
              type="primary" 
              size="small"
              :disabled="countdown > 0"
              @click="sendCode"
              class="send-code-btn"
            >
              {{ countdown > 0 ? `${countdown}s` : '发送验证码' }}
            </nut-button>
          </div>
        </div>

        <div class="form-item">
          <label class="form-label">验证码</label>
          <nut-input 
            v-model="formData.code" 
            placeholder="请输入验证码"
            type="text"
            class="form-input"
          />
        </div>

        <div class="form-item">
          <label class="form-label">用户名</label>
          <nut-input 
            v-model="formData.username" 
            placeholder="请设置用户名"
            type="text"
            class="form-input"
          />
        </div>

        <div class="form-item">
          <label class="form-label">密码</label>
          <nut-input 
            v-model="formData.password" 
            placeholder="请设置密码"
            type="password"
            class="form-input"
          />
        </div>

        <div class="form-item">
          <label class="form-label">确认密码</label>
          <nut-input 
            v-model="formData.confirmPassword" 
            placeholder="请再次输入密码"
            type="password"
            class="form-input"
          />
        </div>

        <div class="form-options">
          <nut-checkbox v-model="agreeTerms" label="我已阅读并同意">
            <span class="terms-link" @click="goToTerms">《用户协议》</span>
            和
            <span class="terms-link" @click="goToPrivacy">《隐私政策》</span>
          </nut-checkbox>
        </div>

        <div class="form-actions">
          <nut-button 
            type="primary" 
            size="large"
            :loading="loading"
            :disabled="!isFormValid || !agreeTerms"
            @click="handleRegister"
            class="register-btn"
          >
            {{ loading ? '注册中...' : '立即注册' }}
          </nut-button>
        </div>

        <div class="login-link">
          <span>已有账户？</span>
          <span class="link" @click="goToLogin">立即登录</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { Left } from '@nutui/icons-vue'

const router = useRouter()
const userStore = useUserStore()

// 表单数据
const formData = reactive({
  phone: '',
  code: '',
  username: '',
  password: '',
  confirmPassword: ''
})

// 同意条款
const agreeTerms = ref(false)

// 加载状态
const loading = ref(false)

// 倒计时
const countdown = ref(0)

// 表单验证
const isFormValid = computed(() => {
  return formData.phone && 
         formData.code && 
         formData.username && 
         formData.password && 
         formData.confirmPassword &&
         formData.password === formData.confirmPassword
})

// 返回上一页
const goBack = () => {
  router.go(-1)
}

// 发送验证码
const sendCode = async () => {
  if (!formData.phone) {
    showToast('请先输入手机号')
    return
  }

  if (!/^1[3-9]\d{9}$/.test(formData.phone)) {
    showToast('请输入正确的手机号')
    return
  }

  try {
    // 模拟发送验证码
    showToast('验证码已发送')
    startCountdown()
  } catch (error) {
    showToast('发送失败，请重试')
  }
}

// 开始倒计时
const startCountdown = () => {
  countdown.value = 60
  const timer = setInterval(() => {
    countdown.value--
    if (countdown.value <= 0) {
      clearInterval(timer)
    }
  }, 1000)
}

// 处理注册
const handleRegister = async () => {
  if (!isFormValid.value) {
    showToast('请填写完整的注册信息')
    return
  }

  if (!agreeTerms.value) {
    showToast('请先同意用户协议和隐私政策')
    return
  }

  if (formData.password !== formData.confirmPassword) {
    showToast('两次输入的密码不一致')
    return
  }

  loading.value = true

  try {
    const result = await userStore.register(formData)
    if (result.success) {
      showToast('注册成功')
      // 跳转到登录页
      setTimeout(() => {
        router.push('/login')
      }, 1000)
    }
  } catch (error) {
    showToast(error.message || '注册失败，请重试')
  } finally {
    loading.value = false
  }
}

// 跳转到登录页
const goToLogin = () => {
  router.push('/login')
}

// 跳转到用户协议
const goToTerms = () => {
  showToast('用户协议页面开发中')
}

// 跳转到隐私政策
const goToPrivacy = () => {
  showToast('隐私政策页面开发中')
}

// 显示提示
const showToast = (message) => {
  console.log(message)
}
</script>

<style scoped>
.header {
  background: white;
  padding: 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #f0f0f0;
  position: sticky;
  top: 0;
  z-index: 100;
}

.back-btn {
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: #666;
}

.title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin: 0;
}

.placeholder {
  width: 24px;
}

.register-form {
  flex: 1;
  padding: 32px 24px;
}

.form-header {
  text-align: center;
  margin-bottom: 40px;
}

.form-header h2 {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin: 0 0 8px 0;
}

.form-header p {
  font-size: 14px;
  color: #666;
  margin: 0;
}

.form-content {
  max-width: 400px;
  margin: 0 auto;
}

.form-item {
  margin-bottom: 24px;
}

.form-label {
  display: block;
  font-size: 14px;
  color: #333;
  margin-bottom: 8px;
  font-weight: 500;
}

.phone-input {
  display: flex;
  gap: 12px;
  align-items: flex-end;
}

.form-input {
  flex: 1;
}

.send-code-btn {
  height: 40px;
  white-space: nowrap;
}

.form-options {
  margin-bottom: 32px;
}

.terms-link {
  color: #ff4757;
  cursor: pointer;
}

.terms-link:hover {
  text-decoration: underline;
}

.form-actions {
  margin-bottom: 24px;
}

.register-btn {
  width: 100%;
  height: 48px;
  font-size: 16px;
  font-weight: 600;
}

.login-link {
  text-align: center;
  font-size: 14px;
  color: #666;
}

.link {
  color: #ff4757;
  cursor: pointer;
  margin-left: 4px;
}

.link:hover {
  text-decoration: underline;
}
</style>
